﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Models;
using Hisun.Controllers;

namespace Hisun.View.SCRB
{
    public partial class Scrb_Report_MonthQuery : System.Web.UI.Page
    {
        HisunDBDataContext db = new HisunDBDataContext();
        public string alert;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var CurrentUserCenterId = (from a in db.TAbc_JS_Users where a.操作员ID == Session[ServerVars.sysSessionUserName].ToString() select new { a.责任部门ID }).Single();

                DropDownList_CenterId.Items.Clear();

                FillData2DropDownList(Convert.ToInt32(CurrentUserCenterId.责任部门ID));

                AbcFullAccountPeriod MyFullAp = new AbcFullAccountPeriod();

                MyFullAp.FullAccountPeriodToDropdownlist(DropDownList_Year, DropDownList_Month, Application["accountPeriod"].ToString());

            }
        }


        /// <summary>
        ///   查询当前操作员对应的作业中心列表
        /// </summary>
        /// <param name="CenterID"></param>
        private void FillData2DropDownList(int CenterID)
        {
            ChildCenterList myCenter = new ChildCenterList(CenterID);

            var myCenterFilters = myCenter.CostCenterlist.Where(p => new string[] { "BF", "BG", "BZ" }.Contains(p.类型) && p.停用 == false);


            foreach (var MylistItem in myCenterFilters)
            {

                ListItem InsertItem = new ListItem(MylistItem.作业中心名称, MylistItem.作业中心ID.ToString());
                DropDownList_CenterId.Items.Add(InsertItem);
            }


        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string MyQueryAccountPeriod = DropDownList_Year.SelectedValue.ToString() + DropDownList_Month.SelectedValue.ToString(); ;
  

            string CurrentUser = Session[ServerVars.sysSessionUserName].ToString();

            var IsValidString = from a in db.TABC_JS_AccountPeriod
                                where a.会计期间ID == MyQueryAccountPeriod
                                select a;
            if (IsValidString.Count() == 0)
            {
               
                alert = "<script>alert('会计期间不存在，请检查输入是否正确');</script>";
                return;
            }
            else if (IsValidString.Count() == 1)
            {
              
                int MyCenterID = Convert.ToInt32(DropDownList_CenterId.SelectedValue);
                string MyPIP = DropDownList_PIP.SelectedValue;
   
                ScrbReport MyReport = new ScrbReport();
                string myHtmlReport = "";
                myHtmlReport = MyReport.WriteExcel_Report_MonthQuery(MyCenterID, MyPIP, MyQueryAccountPeriod, CurrentUser);
           

                if (myHtmlReport != "-1")
                {
                    Response.Redirect("/Report/UserReport/" + myHtmlReport + ".html");
                }
                else
                {
                    alert = "<script>alert('数据为空');</script>";
                }

            }
        }


    }
}
